import { RoleItem } from '/types/role';
import { PageQueryParamType, pageRequest, sendPostRequest, sendGetJsonRequest } from '@/utils/requestEx';

/**
 * 分页查询参条件
 * @param paramName 可输，参数名称
 * @param paramKey 可输，参数键名
 */
export interface RoleQueryType extends PageQueryParamType {
  roleCode?: string;
  roleName?: string;
}

/**
 * 角色授权查询对象
 */
type RoleGrantQueryType = {
  roleId: string
}

type RoleUserQueryType = {
  roleId: string
}

export async function fetchAllRole(params: RoleQueryType) {
  const endPointURI = '/dev/role/list';
  return pageRequest(endPointURI, params);
}

export async function addRole(params: RoleItem) {
  const endPointURI = '/dev/role/add';
  return sendPostRequest(endPointURI, params);
}

export async function updateRole(params: RoleItem) {
  const endPointURI = '/dev/role/update';
  return sendPostRequest(endPointURI, params);
}

export async function deleteRoles(params: RoleItem) {
  const endPointURI = '/dev/role/delete';
  return sendPostRequest(endPointURI, params);
}

export async function fetchMenuTreeByRole(params: RoleGrantQueryType) {
  const { roleId } = params;
  const endPointURI = `/dev/role/menu/tree/${roleId}`;
  return sendGetJsonRequest(endPointURI, params);
}

export async function grantMenus(params: {}) {
  const endPointURI = '/dev/role/allot/menu';
  return sendPostRequest(endPointURI, params);
}

export async function fetchApiTreeByRole(params: RoleGrantQueryType) {
  const { roleId } = params;
  const endPointURI = `/dev/role/api/tree/${roleId}`;
  return sendGetJsonRequest(endPointURI, params);
}

export async function grantApis(params: {}) {
  const endPointURI = '/dev/role/allot/api';
  return sendPostRequest(endPointURI, params);
}

export async function fetchUsersByRole(params: RoleUserQueryType) {
  const { roleId } = params;
  const endPointURI = `/dev/role/user/${roleId}`;
  return sendGetJsonRequest(endPointURI, params);
}